<?php

namespace oa\modules\privilege\controllers;

use oa\services\MenuService;
use Yii;
use oa\models\Role;
use oa\services\RoleService;

class RoleController extends BaseController
{
	public function actionIndex()
	{
        if ($this->request->isAjax) {
            return RoleService::getList();
        }
        return $this->fetch();
	}

	public function actionAdd()
	{
		if ($this->request->isPost) {
			$res = RoleService::save();
			if ($res === true) {
				return $this->ok('提交成功');
			}
			return $this->err('提交失败', $res);
        }
		return $this->fetch(['menus' => MenuService::getAll()], 2);
	}

	public function actionEdit()
	{
		$id = $this->input('id');
		if ($this->request->isPost) {
			$res = RoleService::save($id);
			if ($res === true) {
				return $this->ok('提交成功');
			}
			return $this->err($res);
        }
		$data = RoleService::one($id, 1);
		return $this->fetch([
            'menus' => MenuService::getAll(),
            'data' => $data
        ], 2);
	}

    public function actionAssignMenu()
    {
        $id = $this->input('id');
        if ($this->request->isPost) {
            $res = RoleService::save($id);
            if ($res === true) {
                return $this->ok('提交成功');
            }
            return $this->err($res);
        }
        $data = RoleService::one($id, 1);
        return $this->fetch([
            'menus' => MenuService::getAll(),
            'data' => $data
        ], 2);
    }

	public function actionRemove()
	{
		if ($this->request->isAjax) {
			if (RoleService::remove($this->input('id'))) {
				return $this->ok('删除成功');
			}
			return $this->err('删除失败');
        }
	}

	public function actionDisable()
	{
		if ($this->request->isAjax) {
			if (RoleService::disable($this->input('id'))) {
				return $this->ok('禁用成功');
			}
			return $this->err('禁用失败');
        }
	}

	public function actionEnable()
	{
		if ($this->request->isAjax) {
			if (RoleService::enable($this->input('id'))) {
				return $this->ok('启用成功');
			}
			return $this->err('启用失败');
        }
	}
	
}
